package com.lsz.deport.ui.fragment;

import android.os.Bundle;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.common.event.OrderCancelEvent;
import com.common.mvp.BaseMvpFragment;
import com.common.widght.list.CustomPtrHeader;
import com.common.widght.list.CustomView;
import com.lsz.deport.R;
import com.lsz.deport.json.ContentBean;
import com.lsz.deport.json.OrderContentBean;
import com.lsz.deport.json.ProductChoiceBean;
import com.lsz.deport.ui.order.OrderListActivity;
import com.util_code.dialog.LoadingDialog;
import com.util_code.widget.recycleadpter.BaseRecycleViewAdapter;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import in.srain.cube.views.ptr.PtrClassicFrameLayout;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;

public class DeliveryFragment extends BaseMvpFragment<OrderFmtView, OrderFmtPresent> implements OrderFmtView {

    @BindView(R.id.ptr_refresh)
    PtrClassicFrameLayout ptrRefresh;
    @BindView(R.id.rv_process_list)
    RecyclerView rvProcessList;

    private OrderStatusAdapter statusAdapter;

    private int pageSize = 20;
    private int pageNumber = 1;
    private int status = 0;
    private String dataTime;
    private View layoutEmptyView;

    private LoadingDialog loadingDialog;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_item_list;
    }

    @Override
    protected void initInject() {
        getFragmentComponent().inject(this);
    }

    @Override
    protected void setupView(View rootView) {
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        rvProcessList.setLayoutManager(layoutManager);
        statusAdapter = new OrderStatusAdapter(getActivity());
        rvProcessList.setAdapter(statusAdapter);
        statusAdapter.setOnLoadMoreListener(loadMoreListener, rvProcessList);
        statusAdapter.setOnClickItemListener(itemClickListener);
        setPtrFreshView();
        layoutEmptyView = CustomView.getLayoutEmptyView(getActivity(), 0, "暂无数据", R.color.c_1B1B1B, R.drawable.layout_radius_gray_white_5, onClickListener);

        if (loadingDialog == null) {
            loadingDialog = new LoadingDialog();
        }
    }

    @Override
    protected void setupData(Bundle savedInstanceState) {
        if (!EventBus.getDefault().isRegistered(this)) {//加上判断
            EventBus.getDefault().register(this);
        }
        if (!loadingDialog.isVisible()) {
            loadingDialog.show(getChildFragmentManager(), "loadingdialog");
        }
        getPresenter().getOrderList(pageSize, pageNumber, status);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (EventBus.getDefault().isRegistered(this)) {//加上判断
            EventBus.getDefault().unregister(this);
        }
    }

    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            getPresenter().getOrderList(pageSize, pageNumber, status);
        }
    };

    private void setPtrFreshView() {
        CustomPtrHeader customPtrHeader = new CustomPtrHeader(getActivity());
        ptrRefresh.setHeaderView(customPtrHeader);
        ptrRefresh.addPtrUIHandler(customPtrHeader);
        ptrRefresh.disableWhenHorizontalMove(true);
        ptrRefresh.setPtrHandler(ptrDefaultHandler);
    }

    private OrderStatusAdapter.OrderOnItemClickListener itemClickListener = new OrderStatusAdapter.OrderOnItemClickListener() {
        @Override
        public <T> void onItemClick(View view, T t, int type) {
            OrderContentBean content = (OrderContentBean) t;
            if (type == 1) {
                if (!loadingDialog.isVisible()) {
                    loadingDialog.show(getChildFragmentManager(), "loadingdialog");
                }
                getPresenter().putOrderStatus(content.orderId, 1);
            } else if(type == 0){
                OrderListActivity.startActivity(getActivity(), content.orderId);
            }
        }
    };

    private PtrDefaultHandler ptrDefaultHandler = new PtrDefaultHandler() {
        @Override
        public void onRefreshBegin(final PtrFrameLayout frame) {
            pageNumber = 1;
            getPresenter().getOrderList(pageSize, pageNumber, status);
        }

        @Override
        public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
            return PtrDefaultHandler.checkContentCanBePulledDown(frame, content, header);
        }
    };

    private BaseRecycleViewAdapter.OnLoadMoreListener loadMoreListener = new BaseRecycleViewAdapter.OnLoadMoreListener() {
        @Override
        public void onLoadMore() {
            pageNumber++;
            getPresenter().getOrderList(pageSize, pageNumber, status);
        }
    };

    @Override
    public <T> void onSuccess(T t) {
        ptrRefresh.refreshComplete();
        if (loadingDialog.isVisible()) {
            loadingDialog.dismissAllowingStateLoss();
        }
        if(t instanceof ProductChoiceBean){
            ProductChoiceBean choiceBean = (ProductChoiceBean) t;
            int listSize = choiceBean.content.size();
            pageNumber = Integer.parseInt(choiceBean.number) == 0 ? 1 : Integer.parseInt(choiceBean.number);
            if (pageNumber == 1) {
                if (listSize == 0) {
                    statusAdapter.setEmptyView(layoutEmptyView);
                    statusAdapter.loadMoreEnd(false);
                } else if (listSize < 5) {
                    statusAdapter.loadMoreEnd(false);
                }
                statusAdapter.resetData(choiceBean.content);
            } else {
                if (listSize == 0) {
                    statusAdapter.loadMoreEnd(false);
                } else if (listSize < 5) {
                    statusAdapter.addData(choiceBean.content);
                    statusAdapter.loadMoreComplete();
                    statusAdapter.loadMoreEnd(false);

                } else {
                    statusAdapter.addData(choiceBean.content);
                    statusAdapter.loadMoreComplete();
                }
            }
        }

    }

    @Override
    public void onFail() {
        ptrRefresh.refreshComplete();
        if (loadingDialog.isVisible()) {
            loadingDialog.dismissAllowingStateLoss();
        }
        if (statusAdapter.getData().size() > 0) {
            statusAdapter.loadMoreEnd(false);
        } else {
            statusAdapter.setEmptyView(CustomView.getLayoutView());
            statusAdapter.loadMoreEnd(false);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(OrderCancelEvent event) {
        pageNumber = 1;
        getPresenter().getOrderList(pageSize, pageNumber, status);
    }
}
